package com.feature50.makeover.whackachet;

import com.feature50.makeover.utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class Chet extends JComponent {
    private BufferedImage chet;

    private boolean chetShowing = false;
    private boolean hit = false;

    private float built = 0f;

    public Chet() {
        chet = ImageUtils.getImage("chet.png");
    }

    public float getBuilt() {
        return built;
    }

    public void setBuilt(float built) {
        this.built = built;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(chet.getWidth(), chet.getHeight());
    }

    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    @Override
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }

    @Override
    protected void paintComponent(Graphics g) {
        int y = chet.getHeight() - (int) (built * chet.getHeight());
        int x = 0;

        g.drawImage(chet, x, y, null);

        if (hit) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setFont(new Font("Arial", Font.PLAIN, 80));
            g2d.setColor(Color.RED);

            int width = (int) g2d.getFontMetrics().getStringBounds("X", g2d).getWidth();
            int height = g2d.getFontMetrics().getHeight();

            g2d.drawString("X", (getWidth() / 2) - (width / 2), (getHeight()/  2) + (height / 2));
        }
    }

    public boolean isChetShowing() {
        return chetShowing;
    }

    public void setChetShowing(boolean chetShowing) {
        this.chetShowing = chetShowing;
    }

    public void setHit(boolean b) {
        this.hit = b;
    }

    public boolean isHit(int cx, int cy, int ccx, int ccy) {
        Point p1 = new Point(cx, cy);
        SwingUtilities.convertPointFromScreen(p1, this);

        Point p2 = new Point(ccx, ccy);
        SwingUtilities.convertPointFromScreen(p2, this);

        if (built < 0.9f) return false;


        if (p2.x < 0) return false;
        if (p1.x > getWidth()) return false;
        if (p1.y > getHeight()) return false;
        if (p2.y < 0) return false;

        return true;
    }
}
